package com.yohann.aliyun_service.controller;

import com.yohann.aliyun_service.service.AliyunService;
import com.yohann.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 阿里云服务控制器
 * </p>
 *
 * @author Yohann
 * @since 2020/10/19 16:42
 */
@Api(tags = "阿里云服务")
@RestController
@RequestMapping("/aliyun-service")
public class AliyunController {
    @Autowired
    private AliyunService aliyunService;

    /**
     * 上传照片
     *
     * @param file 照片
     * @return 存储url
     */
    @ApiOperation("上传照片")
    @PostMapping("uploadImage")
    public Result uploadImage(@ApiParam("照片") MultipartFile file) {
        String url = aliyunService.uploadFileImage(file);
        System.out.println(url);

        return Result.ok().data("url", url);
    }

    /**
     * 上传视频
     *
     * @param file 视频
     * @return 返回视频id+url
     */
    @ApiOperation("上传视频")
    @PostMapping("uploadVideo")
    public Result uploadVideo(@ApiParam("视频") MultipartFile file) {
        String id = aliyunService.uploadVideo(file);
        String url = aliyunService.getVideoUrl(id);

        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("url", url);

        return Result.ok().data(map);
    }

    /**
     * 删除阿里云中的视频
     *
     * @param id 视频id
     * @return 删除状态
     */
    @ApiOperation("删除阿里云中的视频")
    @GetMapping("delete/{id}")
    public Result deleteVideo(@ApiParam("视频id") @PathVariable String id) {
        if ("null".equals(id)) {
            return Result.ok();
        }
        aliyunService.deleteVideo(id);

        return Result.ok();
    }

    /**
     * 批量删除视频
     *
     * @param videoIdList id集合
     * @return 删除状态
     */
    @ApiOperation("批量删除视频")
    @DeleteMapping("delete-batch")
    public Result deleteBatch(@ApiParam("id集合") @RequestParam("videoIdList") List<String> videoIdList) {
        String ids = StringUtils.join(videoIdList.toArray(), ",");
        aliyunService.deleteVideo(ids);

        return Result.ok();
    }

    /**
     * 发送验证码
     *
     * @param phone 手机号
     * @return 发送状态
     */
    @ApiOperation("发送验证码")
    @GetMapping("sendCode/{phone}")
    public Result sendCode(@ApiParam("手机号") @PathVariable String phone) {
        return aliyunService.sendCode(phone);
    }

    /**
     * 校验验证码
     *
     * @param phone 手机号
     * @param code  验证码
     * @return 验证结果
     */
    @ApiOperation("校验验证码")
    @GetMapping("checkCode/{phone}/{code}")
    public Result checkCode(@ApiParam("手机号") @PathVariable String phone, @ApiParam("验证码") @PathVariable String code) {
        return aliyunService.checkCode(phone, code);
    }
}